package controllers;

import java.util.List;

import models.my.MyFavorite;
import models.my.MyFriend;
import models.my.MyMessage;
import models.my.MyPosts;
import models.user.BaseUserInfo;
import models.user.EduUserInfo;
import models.user.LinkUserInfo;
import models.user.PersonUserInfo;
import models.user.PortraitUserInfo;
import models.user.UserInfo;
import models.user.WorkUserInfo;
import play.mvc.Controller;
import play.mvc.Result;
import utils.MengException;
import views.html.*;

public class Application extends Controller {

	public static Result index() {
		String username = session().get("username");

		try {
			List<MyPosts> posts = MyPosts.getInstances().getPostsPage(username, 0, 10);
			List<UserInfo> uis = UserInfo.getInstances().getUserInfo(0, 10);
			return ok(index.render(posts,uis));
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return ok("get Posts failure");
	}

	public static Result login() {
		return ok(login.render(""));
	}

	public static Result register() {
		return ok(register.render(""));
	}

	public static Result myfriend() {
		
		String username = session().get("username");
		try {
			List<MyFriend> friends = MyFriend.getInstances().getFriendPage(username, 0, 10);
			List<UserInfo> uis = UserInfo.getInstances().getUserInfo(0, 10);
			return ok(myfriend.render(friends,uis));
			
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ok("get Friends Failure");
	}

	public static Result myfavorite() {
		
		String username = session().get("username");
		try {
			List<MyFavorite> favorites = MyFavorite.getInstances().getFavoritePage(username, 0, 10);
			List<UserInfo> uis = UserInfo.getInstances().getUserInfo(0, 10);
			return ok(myfavorite.render(favorites,uis));
			
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ok("get Friends Failure");
	}

	public static Result mymsg() {
		String username = session().get("username");
		try {
			List<MyMessage> message = MyMessage.getInstances().getMessagePage(username, 0, 10);
			PortraitUserInfo ptui = PortraitUserInfo.getInstances().getPUI(username); 
			return ok(mymsg.render(message,ptui));
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ok("get Message failure");
	}

	public static Result myprofile() {
		
		String username = session().get("username");
		try {
			BaseUserInfo bui = BaseUserInfo.getInstances().getBaseUserInfo(username);
			LinkUserInfo lui = LinkUserInfo.getInstances().getLUI(username);
			EduUserInfo eui = EduUserInfo.getInstances().getEUI(username);
			PersonUserInfo pui = PersonUserInfo.getInstances().getPSUI(username);
			WorkUserInfo wui = WorkUserInfo.getInstances().getWUI(username);
			PortraitUserInfo ptui = PortraitUserInfo.getInstances().getPUI(username); 
			
			return ok(myprofile.render(bui,lui,eui,pui,wui,ptui));
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ok("get UserInfo failure");
	}
	
	public static Result mysafe() {
		
		String username = session().get("username");
		try {
			PortraitUserInfo ptui = PortraitUserInfo.getInstances().getPUI(username); 
			LinkUserInfo lui = LinkUserInfo.getInstances().getLUI(username);
			return ok(mysafe.render(ptui,lui));
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ok("get mysafe failure");
	}

}
